import os


def extract_target_lines(file_path):
    with open(file_path, encoding='utf-8', mode='r', errors='ignore') as file:
        lines = file.readlines()
        is_target = False
        result_lines = []
        target_paragraph = []
        for line in lines:
            if ("20000 识别下发" in line):
                is_target = True
                target_paragraph = []
            if (is_target):
                print(line)
                target_paragraph.append(line)
            if ("start filter text" in line and is_target):
                print(line)
                target_paragraph.append(line)
                target_paragraph.append("\r\n")
                # 进入结果集
                result_lines.extend(target_paragraph)
                # 清空
                target_paragraph = []
                is_target = False
        # 写结果
        with open('result.txt', 'w', encoding='utf-8', errors='ignore') as file:
            file.writelines(result_lines)


if __name__ == '__main__':
    # dir_path = "D:/download/主驾误唤醒-车机日志"
    # for filepath, dirnames, filenames in os.walk(dir_path):
    #     for filename in filenames:
    #         if not filename.endswith(".log"):
    #             continue
    extract_target_lines("D:/i讯飞/IFLY/localfiles/emtang/File/2024-09/主驾人声干扰日志.txt")
